package com.youdianstar.app.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView

import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.common.ex.setThrottleListener
import com.youdianstar.app.common.util.GsonUtil
import com.youdianstar.app.R
import com.youdianstar.app.entity.OpenItem
import com.youdianstar.app.entity.OpenTime
import com.youdianstar.app.util.DataHelperEx
import com.youdianstar.app.common.widget.SwitchButton


/**
 * File: OpenTimeAdapter
 * Author: 82149 Create: 2022/3/29 19:59
 * Changes (from 2022/3/29)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class ShopTimeAdapter(val context: Context, var isFrozen:Boolean = false, var datas: MutableList<OpenItem>, val listener:OnActionListener) :
    RecyclerView.Adapter<ShopTimeAdapter.TimeHolder>() {
    interface OnActionListener{
        fun onItemPartSelect(position:Int,tvTime:TextView,item:OpenItem,index:Int)
    }

    class TimeHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvWeekTitle by lazy {
            itemView.findViewById<TextView>(R.id.tx_Weekday)
        }
        val swcWeekTime by lazy {
            itemView.findViewById<SwitchButton>(R.id.swc_WeekTime)
        }
        val txDay24 by lazy {
            itemView.findViewById<TextView>(R.id.tx_day24)
        }
        val swcDay24 by lazy {
            itemView.findViewById<SwitchButton>(R.id.swc_day24Time)
        }
        val rlPart01 by lazy {
            itemView.findViewById<RelativeLayout>(R.id.rl_rangePart1)
        }
        val rlPart02 by lazy {
            itemView.findViewById<RelativeLayout>(R.id.rl_rangePart2)
        }
        val rlPart03 by lazy {
            itemView.findViewById<RelativeLayout>(R.id.rl_rangePart3)
        }
        val rlPart04 by lazy {
            itemView.findViewById<RelativeLayout>(R.id.rl_rangePart4)
        }
        val ivDelete1 by lazy {
            itemView.findViewById<ImageView>(R.id.iv_delete_Part1)
        }
        val ivDelete2 by lazy {
            itemView.findViewById<ImageView>(R.id.iv_delete_Part2)
        }
        val ivDelete3 by lazy {
            itemView.findViewById<ImageView>(R.id.iv_delete_Part3)
        }
        val ivDelete4 by lazy {
            itemView.findViewById<ImageView>(R.id.iv_delete_Part4)
        }

        val ivMore1 by lazy {
            itemView.findViewById<ImageView>(R.id.iv_more_Part1)
        }
        val ivMore2 by lazy {
            itemView.findViewById<ImageView>(R.id.iv_more_Part2)
        }
        val ivMore3 by lazy {
            itemView.findViewById<ImageView>(R.id.iv_more_Part3)
        }
        val ivMore4 by lazy {
            itemView.findViewById<ImageView>(R.id.iv_more_Part4)
        }
        val ivAdd1 by lazy {
            itemView.findViewById<ImageView>(R.id.iv_add_Part1)
        }
        val ivAdd2 by lazy {
            itemView.findViewById<ImageView>(R.id.iv_add_Part2)
        }
        val ivAdd3 by lazy {
            itemView.findViewById<ImageView>(R.id.iv_add_Part3)
        }
        val ivAdd4 by lazy {
            itemView.findViewById<ImageView>(R.id.iv_add_Part4)
        }
        val tvTimePart1 by lazy {
            itemView.findViewById<TextView>(R.id.tv_time_Part1)
        }
        val tvTimePart2 by lazy {
            itemView.findViewById<TextView>(R.id.tv_time_Part2)
        }
        val tvTimePart3 by lazy {
            itemView.findViewById<TextView>(R.id.tv_time_Part3)
        }
        val tvTimePart4 by lazy {
            itemView.findViewById<TextView>(R.id.tv_time_Part4)
        }
        val rangeContainer by lazy {
            itemView.findViewById<ConstraintLayout>(R.id.rangeContainer)
        }
        val lineBottom by lazy {
            itemView.findViewById<TextView>(R.id.viewLine_item)
        }
        /**
         * 事件意图
         * @param layoutVisi 布局是否显示
         * @param addAction 点击添加->变成显示时间样式
         * @param deleteAble 删除按钮是否显示
         */
        fun setPart1Layout(layoutVisi:Boolean,addAction:Boolean,deleteAble:Boolean){
            rlPart01.visibility = if (layoutVisi) View.VISIBLE else View.GONE
            ivDelete1.visibility = if (addAction && deleteAble) View.VISIBLE else View.INVISIBLE
            ivMore1.visibility = if (addAction) View.VISIBLE else View.INVISIBLE
            tvTimePart1.visibility = if (addAction) View.VISIBLE else View.INVISIBLE
            ivAdd1.visibility = if (addAction) View.INVISIBLE else View.VISIBLE
            if (!addAction){
                tvTimePart1.text =""
            }
        }

        fun setPart2Layout(layoutVisi:Boolean,addAction:Boolean,deleteAble:Boolean){
            rlPart02.visibility = if (layoutVisi) View.VISIBLE else View.GONE
            ivDelete2.visibility = if (addAction && deleteAble) View.VISIBLE else View.INVISIBLE
            ivMore2.visibility = if (addAction) View.VISIBLE else View.INVISIBLE
            tvTimePart2.visibility = if (addAction) View.VISIBLE else View.INVISIBLE
            ivAdd2.visibility = if (addAction) View.INVISIBLE else View.VISIBLE
            if (!addAction){
                tvTimePart2.text =""
            }
        }

        fun setPart3Layout(layoutVisi:Boolean,addAction:Boolean,deleteAble:Boolean){
            rlPart03.visibility = if (layoutVisi) View.VISIBLE else View.GONE
            ivDelete3.visibility = if (addAction && deleteAble) View.VISIBLE else View.INVISIBLE
            ivMore3.visibility = if (addAction) View.VISIBLE else View.INVISIBLE
            tvTimePart3.visibility = if (addAction) View.VISIBLE else View.INVISIBLE
            ivAdd3.visibility = if (addAction) View.INVISIBLE else View.VISIBLE
            if (!addAction)
                tvTimePart3.text =""
        }

        fun setPart4Layout(layoutVisi:Boolean,addAction:Boolean,deleteAble:Boolean){
            rlPart04.visibility = if (layoutVisi) View.VISIBLE else View.GONE
            ivDelete4.visibility = if (addAction && deleteAble) View.VISIBLE else View.INVISIBLE
            ivMore4.visibility = if (addAction) View.VISIBLE else View.INVISIBLE
            tvTimePart4.visibility = if (addAction) View.VISIBLE else View.INVISIBLE
            ivAdd4.visibility = if (addAction) View.INVISIBLE else View.VISIBLE
            if (!addAction)
                tvTimePart4.text =""
        }

        fun setRangeLayoutState(context: Context,isAllDay:Boolean){
            txDay24.alpha = if (isAllDay) 1.0F else 0.4F
            //修改选中状态
            rlPart01.background =
                if (!isAllDay) {
                    ContextCompat.getDrawable(context, R.drawable.shape_circle_date)
                } else ContextCompat.getDrawable(context, R.drawable.shape_circle_date_unable)
            rlPart02.background =
                if (!isAllDay) {
                    ContextCompat.getDrawable(context, R.drawable.shape_circle_date)
                } else ContextCompat.getDrawable(context, R.drawable.shape_circle_date_unable)
            rlPart03.background =
                if (!isAllDay) {
                    ContextCompat.getDrawable(context, R.drawable.shape_circle_date)
                } else ContextCompat.getDrawable(context, R.drawable.shape_circle_date_unable)
            rlPart04.background =
                if (!isAllDay) {
                    ContextCompat.getDrawable(context, R.drawable.shape_circle_date)
                } else ContextCompat.getDrawable(context, R.drawable.shape_circle_date_unable)
            ivDelete1.alpha = if (isAllDay) 0.8F else 1.0F
            ivDelete2.alpha = if (isAllDay) 0.8F else 1.0F
            ivDelete3.alpha = if (isAllDay) 0.8F else 1.0F
            ivDelete4.alpha = if (isAllDay) 0.8F else 1.0F
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TimeHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.layout_shop_time, parent, false)
        return TimeHolder(view)
    }

    override fun onBindViewHolder(holder: TimeHolder, position: Int) {
        datas[position].let {
            when(it.value){
                1-> holder.tvWeekTitle.text = context.getString(R.string.shop_monday)
                2-> holder.tvWeekTitle.text = context.getString(R.string.shop_tuesday)
                3-> holder.tvWeekTitle.text = context.getString(R.string.shop_wednesday)
                4-> holder.tvWeekTitle.text = context.getString(R.string.shop_thursday)
                5-> holder.tvWeekTitle.text = context.getString(R.string.shop_friday)
                6-> holder.tvWeekTitle.text = context.getString(R.string.shop_saturday)
                else ->{
                    holder.tvWeekTitle.text = context.getString(R.string.shop_sunday)
                }
            }
            holder.swcWeekTime.isChecked = it.check
            holder.rangeContainer.visibility = if (it.check) View.VISIBLE else View.GONE
            holder.swcDay24.isChecked = it.allDay
            holder.setRangeLayoutState(context,it.allDay)

            it.initPartList()
            val size = it.partList?.size ?: 0
//            holder.rlPart01.visibility = View.GONE
//            holder.rlPart02.visibility = View.GONE
//            holder.rlPart03.visibility = View.GONE
//            holder.rlPart04.visibility = View.GONE
            if (size==0){
                holder.setPart1Layout(layoutVisi = true,addAction = false,deleteAble = false)
            }
            if (size>0){
                holder.tvTimePart1.text = it.getIndexRange(0).numberTimeRange
                holder.setPart1Layout(layoutVisi = true,addAction = true,deleteAble = false)
            }
            if (size>1){
                holder.tvTimePart2.text = it.getIndexRange(1).numberTimeRange
                holder.setPart2Layout(layoutVisi = true,addAction = true,deleteAble = false)
            }
            if (size>2){
                holder.tvTimePart3.text = it.getIndexRange(2).numberTimeRange
                holder.setPart3Layout(layoutVisi = true,addAction = true,deleteAble = false)
            }
            if (size>3){
                holder.tvTimePart4.text = it.getIndexRange(3).numberTimeRange
                holder.setPart4Layout(layoutVisi = true,addAction = true,deleteAble = false)
            }

            when(size){
                4->{
                    holder.setPart4Layout(layoutVisi = true,addAction = true,deleteAble = true)
                }
                3->{
                    holder.setPart4Layout(layoutVisi = true,addAction = false,deleteAble = false)
                    holder.setPart3Layout(layoutVisi = true,addAction = true,deleteAble = true)
                }
                2->{
                    holder.rlPart04.visibility = View.INVISIBLE
                    holder.setPart3Layout(layoutVisi = true,addAction = false,deleteAble = false)
                    holder.setPart2Layout(layoutVisi = true,addAction = true,deleteAble = true)
                }
                1->{
                    holder.rlPart04.visibility = View.GONE
                    holder.rlPart03.visibility = View.GONE
                    holder.setPart2Layout(layoutVisi = true,addAction = false,deleteAble = false)
                    //1号布局 不能删除 holder.setPart1Layout(layoutVisi = true,addAction = true,deleteAble = true)
                }
                else ->{
                    holder.rlPart04.visibility = View.GONE
                    holder.rlPart03.visibility = View.GONE
                    holder.rlPart02.visibility = View.INVISIBLE
                    holder.setPart1Layout(layoutVisi = true,addAction = false,deleteAble = false)
                }
            }

            holder.lineBottom.visibility = if (position==datas.size-1) View.INVISIBLE else View.VISIBLE
        }

        holder.swcDay24.isEnabled = !isFrozen
        holder.swcWeekTime.isEnabled = !isFrozen
        holder.swcWeekTime.setOnCheckedChangeListener(object : SwitchButton.OnCheckedChangeListener{
            override fun onCheckedChanged(view: SwitchButton?, isChecked: Boolean) {
                holder.rangeContainer.visibility = if (isChecked) View.VISIBLE else View.GONE
                datas[holder.bindingAdapterPosition].check = isChecked
            }
        })

        holder.swcDay24.setOnCheckedChangeListener(object :SwitchButton.OnCheckedChangeListener{
            override fun onCheckedChanged(view: SwitchButton?, isChecked: Boolean) {
                holder.setRangeLayoutState(context,isChecked)
                datas[holder.bindingAdapterPosition].allDay = isChecked
            }
        })

        holder.ivDelete1.setThrottleListener {
            //判断显示逻辑(1-不可删除)
        }

        holder.ivDelete2.setThrottleListener {
            if (isFrozen)
                return@setThrottleListener
            if (datas[position].allDay)
                return@setThrottleListener
            //判断显示逻辑:隐藏布局3,删除第2个,修改part2为添加状态,(设置1可删除-不可删除)
            holder.setPart3Layout(layoutVisi = false,addAction = false,deleteAble = false)
            datas[position].rangeTime2?.clear()
            datas[position].removePart(1)
            holder.setPart2Layout(layoutVisi =true,addAction = false,deleteAble = false)
            notifyItemChanged(position)
        }
        holder.ivDelete3.setThrottleListener {
            if (isFrozen)
                return@setThrottleListener
            if (datas[position].allDay)
                return@setThrottleListener
            //判断显示逻辑:隐藏布局4,删除第三个,修改part3为添加状态,设置2可删除
            holder.setPart4Layout(layoutVisi = false,addAction = false,deleteAble = false)
            datas[position].rangeTime3?.clear()
            datas[position].removePart(2)
            holder.setPart3Layout(layoutVisi =true,addAction = false,deleteAble = false)
            notifyItemChanged(position)
        }
        holder.ivDelete4.setThrottleListener {
            if (isFrozen)
                return@setThrottleListener
            if (datas[position].allDay)
                return@setThrottleListener
            //判断显示逻辑: 删除第四个,修改Part4 为添加状态,设置3可删除
            datas[position].rangeTime4?.clear()
            datas[position].removePart(3)
            holder.setPart4Layout(layoutVisi = true,addAction = false,deleteAble = false)
            notifyItemChanged(position)
        }

        holder.rlPart01.setThrottleListener {
            if (isFrozen)
                return@setThrottleListener
            //判断逻辑: 区间1有数据->显示选择框;区间1无数据->设置为添加状态
            datas[position].let {
                if (it.allDay)
                    return@let
                listener.onItemPartSelect(position,holder.tvTimePart1,it,0)
            }
        }

        holder.rlPart02.setThrottleListener {
            if (isFrozen)
                return@setThrottleListener
            //判断逻辑: 区间2有数据->显示选择框;区间2无数据->设置为添加状态
            datas[position].let {
                if (it.allDay)
                    return@let
                if (it.rangeTime2?.isEmpty() == true){
                    it.partList?.add(it.getDefaultRange())
                    it.rangeTime2?.addAll(DataHelperEx.getRangeList(it.getDefaultRange()))
                    notifyItemChanged(position)
                }else{
                    listener.onItemPartSelect(position,holder.tvTimePart2,it,1)
                }
            }
        }

        holder.rlPart03.setThrottleListener {
            if (isFrozen)
                return@setThrottleListener
            //判断逻辑: 区间3有数据->显示选择框;区间3无数据->设置为添加状态
            datas[position].let {
                if (it.allDay)
                    return@let
                if (it.rangeTime3?.isEmpty() == true){
                    it.partList?.add(it.getDefaultRange())
                    it.rangeTime3?.addAll(DataHelperEx.getRangeList(it.getDefaultRange()))
                    notifyItemChanged(position)
                }else{
                    listener.onItemPartSelect(position,holder.tvTimePart3,it,2)
                }
            }
        }

        holder.rlPart04.setThrottleListener {
            if (isFrozen)
                return@setThrottleListener
            //判断逻辑: 区间4有数据->显示选择框;区间4无数据->设置为添加状态
            datas[position].let {
                if (it.allDay)
                    return@let
                if (it.rangeTime4?.isEmpty() == true){
                    it.partList?.add(it.getDefaultRange())
                    it.rangeTime4?.addAll(DataHelperEx.getRangeList(it.getDefaultRange()))
                    notifyItemChanged(position)
                } else {
                    listener.onItemPartSelect(position,holder.tvTimePart4,it,3)
                }
            }
        }
    }

    override fun getItemCount(): Int {
        return datas.size
    }

    fun update(frozen: Boolean,list:MutableList<OpenItem>){
        if (frozen)
            this.isFrozen = true
        datas.clear()
        datas.addAll(list)
        notifyDataSetChanged()
    }

    fun getOpentimeList():MutableList<OpenTime>{
        return DataHelperEx.copyOpentimeList(datas)
    }

    fun getOpentimeJson():String{
        val jsonList = DataHelperEx.copyOpentimeList(datas)
        return GsonUtil.toJson(jsonList)
    }
}